package com.shujia.flink.source

import org.apache.flink.streaming.api.scala._
import org.apache.flink.api.common.eventtime.WatermarkStrategy
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.connector.kafka.source.KafkaSource
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer

object Demo4KafkaSource {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    /**
     * kafka source  -- 无界流
     */

    val source: KafkaSource[String] = KafkaSource
      .builder[String]
      .setBootstrapServers("master:9092,node1:9092,node2:9092") //指定kafka 节点列表
      .setTopics("bigdata") //指定读取的topic
      .setGroupId("my-group") //指定消费者组
      .setStartingOffsets(OffsetsInitializer.earliest) //读取所有的数据
      //.setStartingOffsets(OffsetsInitializer.latest())//从最末尾位点开始消费
      .setValueOnlyDeserializer(new SimpleStringSchema()) //指定反序列化的类
      .build

    //居于kafka source构建DataStream
    val kafkaDS: DataStream[String] = env.fromSource(source, WatermarkStrategy.noWatermarks(), "Kafka Source")


    kafkaDS.print()

    env.execute()
  }
}
